Imports System.Text.RegularExpressions

Partial Class HardwareKeys

    Inherits System.Web.UI.Page



    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Redirect("AdminOptions.aspx")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim clientname As String = "", username As String = ""

        'Check the validity of the license key and the hardware key

        'Connect to the Access database
'
' Now  "C:\inetpub\vhosts\2676620.cust.peer1.com\private\db\PerClick.mdb       
'
        Dim strConn As String = ConfigurationManager.ConnectionStrings("LocalConnectionString").ConnectionString
        Dim MyConn As New OleDbConnection(strConn)
        MyConn.Open()

        'Check to see if the hardware key has been registered before  ????? 
        Dim MySQL As String = "SELECT Username, ClientName FROM UserData WHERE [ID]=" & Request.QueryString("ID")

        Dim cmd As New OleDbCommand(MySQL, MyConn)
        Dim r As OleDbDataReader = cmd.ExecuteReader()
        r.Read()
        clientname = r.Item("ClientName")
        username = r.Item("Username")

        'Close access to the database
        MyConn.Close()

        ' Place user identification at head of page
        Me.HeaderLabel1.Text = username
        Me.HeaderLabel2.Text = "'" & clientname & "'"

    End Sub

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        'Add delete Yes/No box
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim l As LinkButton = CType(e.Row.FindControl("LinkButton1"), LinkButton)
            Dim JavaConfirm As String = "javascript:" & _
                "q=confirm('Are you sure you want to delete\nthe purchase made on " & _
                DataBinder.Eval(e.Row.DataItem, "DatePurchased") & "?'); this.blur(); return q;"
            l.Attributes.Add("onclick", JavaConfirm)
        End If
    End Sub

    Protected Sub Add_Purchase_Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add_Purchase_Button2.Click
        show_adder(2)
    End Sub

    Sub show_adder(ByVal show_it As Integer)
        Try

            Me.DetailsView1.Visible = Not (show_it = 0)
            Me.Add_Purchase_Button2.Visible = (show_it = 0)
            Me.Button1.Enabled = (show_it = 0)

            'Clear the search fields
            For i As Byte = 1 To 7
                Dim x As String = Split("purchasedate PCName HardwareKey clickspurchased expirydate notify comments")(i - 1) & "_txtbox1"
                Dim MyTextBox As TextBox = CType(Me.DetailsView1.FindControl(x), TextBox)
                Select Case i
                    Case 1
                        MyTextBox.Text = Now.ToString("dd/MM/yyyy")
                    Case 4
                        MyTextBox.Text = "0"
                    Case 5
                        MyTextBox.Text = Now.AddYears(1).ToString("dd/MM/yyyy")
                    Case Else
                        MyTextBox.Text = ""
                End Select
            Next i

            Dim MyListBox As ListBox = CType(Me.DetailsView1.FindControl("type_txtbox1"), ListBox)
            MyListBox.SelectedIndex = 0

            Dim MyTextBox1 As TextBox = CType(Me.DetailsView1.FindControl("clickspurchased_txtbox1"), TextBox)
            Dim MyTextBox2 As TextBox = CType(Me.DetailsView1.FindControl("expirydate_txtbox1"), TextBox)
            Dim chkUnlimited As CheckBox = CType(Me.DetailsView1.FindControl("unlimitedclicks_txtbox1"), CheckBox)
            chkUnlimited.Checked = False
            MyTextBox1.Visible = Not (show_it = 0)
            MyTextBox2.Visible = MyTextBox1.Visible
        Catch ex As Exception
            Response.Write(ex.ToString)
        End Try
    End Sub

    Protected Sub DetailsView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted
        Try
            show_adder(False)
        Catch ex As Exception
            Response.Write(ex.Message.ToString)
        End Try

    End Sub

    Protected Sub DetailsView1_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewModeEventArgs) Handles DetailsView1.ModeChanging
        If e.CancelingEdit Then show_adder(False)
    End Sub

    Protected Function type_evaluator(ByVal myinput As Object) As String
        Dim outputStr As String = ""
        If myinput Is DBNull.Value Then
            outputStr = ""
        Else
            Select Case Integer.Parse(myinput.ToString)
                Case 0
                    outputStr = "Road level"
                Case 1
                    outputStr = "Premise level"
                Case 2
                    outputStr = "Map ref"
                Case 3
                    outputStr = "Place list"
                Case 4
                    outputStr = "Street list"
            End Select
        End If
        Return outputStr
    End Function

    Function YesNo(ByVal x As Object) As String
        If Not x Is DBNull.Value Then
            If x Then Return "Yes"
        End If
        Return "No"
    End Function

    Function date_formatter(ByVal mystring As String) As String
        'Flip month and day
        mystring = mystring.Replace("\", "/")
        Dim datearray As Array = Split(mystring, "/")
        mystring = datearray(1) & "/" & datearray(0) & "/" & datearray(2)
        'mystring = datearray(2) & "-" & datearray(1) & "-" & datearray(0)
        'Add hash symbols
        mystring = "#" & mystring & "#"
        'mystring = "2012/05/23"
        Return mystring
    End Function

    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting
        Try
            Dim InsertString As StringBuilder = New StringBuilder()

            'InsertString.Append("INSERT INTO History (UserID, DatePurchased,PCName, HardwareKey, ClicksPurchased,ExpiryDate,UnlimitedClicks, [Type], Areas, Notify,Comments) VALUES (" & Request.QueryString("ID"))
            InsertString.Append("INSERT INTO History (UserID, DatePurchased, ClicksPurchased, ExpiryDate, Comments, [Type], UnlimitedClicks, Notify, Areas, PCName, HardwareKey) VALUES (" & Request.QueryString("ID"))
            Dim TextboxNames As Array = Split("purchasedate clickspurchased expirydate comments type unlimitedclicks notify areas PCName HardwareKey")

            For Each i As String In TextboxNames

                Dim mystring As String = ""
                Select Case i
                    Case "unlimitedclicks"
                        mystring = CType(Me.DetailsView1.FindControl(i & "_txtbox1"), CheckBox).Checked
                    Case "type"
                        mystring = CType(Me.DetailsView1.FindControl(i & "_txtbox1"), ListBox).Text
                    Case Else
                        mystring = CType(Me.DetailsView1.FindControl(i & "_txtbox1"), TextBox).Text
                End Select

                If i = "comments" Or i = "areas" Or i = "PCName" Or i = "HardwareKey" Then _
                    mystring = """" & mystring & """"
                If i = "purchasedate" Or i = "expirydate" Then _
                    mystring = date_formatter(mystring)
                If (i = "clickspurchased" Or i = "notify") And mystring = "" Then _
                    mystring = "0"

                InsertString.Append(", " & mystring)

            Next i
            InsertString.Append(")")

            Me.SqlDataSource1.InsertCommand = InsertString.ToString
        Catch ex As Exception
            Response.Write(ex.ToString)
        End Try
    End Sub

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating

        Dim MyTextBox As TextBox, MyCheckBox As CheckBox, MyListBox As ListBox, MyHiddenField As HiddenField
        Dim UpdateString As StringBuilder = New StringBuilder("UPDATE History SET ")

        For Each row As GridViewRow In GridView1.Rows
            ' Try to get find a checkbox
            Try
                ' Selects the checkbox which is inside the GridView control
                MyCheckBox = CType(row.FindControl("UnlimitedClicks_checkbox"), CheckBox)
                UpdateString.Append("UnlimitedClicks=" & MyCheckBox.Checked.ToString & ",")

                'Comments
                MyTextBox = CType(row.FindControl("Comments_textbox"), TextBox)
                UpdateString.Append("Comments=" & """" & MyTextBox.Text & """" & ", ")

                'Areas
                MyTextBox = CType(row.FindControl("Areas_textbox"), TextBox)
                UpdateString.Append("Areas=" & """" & MyTextBox.Text & """" & ", ")

                'PCName
                MyTextBox = CType(row.FindControl("PCName_textbox"), TextBox)
                UpdateString.Append("PCName=" & """" & MyTextBox.Text & """" & ", ")

                'HardwareKey
                MyTextBox = CType(row.FindControl("HardwareKey_textbox"), TextBox)
                UpdateString.Append("HardwareKey=" & """" & MyTextBox.Text & """" & ", ")

                'Type
                MyListBox = CType(row.FindControl("Type_listbox"), ListBox)
                UpdateString.Append("[Type]=" & MyListBox.SelectedValue & ", ")

                'Expiry date
                MyTextBox = CType(row.FindControl("ExpiryDate_textbox"), TextBox)
                UpdateString.Append("ExpiryDate=" & date_formatter(MyTextBox.Text) & ", ")

                'DatePurchased
                MyTextBox = CType(row.FindControl("DatePurchased_textbox"), TextBox)
                UpdateString.Append("DatePurchased=" & date_formatter(MyTextBox.Text) & ", ")

                'Notify
                MyTextBox = CType(row.FindControl("Notify_textbox"), TextBox)
                Dim mystring As String = MyTextBox.Text
                If mystring = "" Then mystring = "0"
                UpdateString.Append("Notify=" & mystring & ", ")

                'ClicksPurchased
                MyTextBox = CType(row.FindControl("ClicksPurchased_textbox"), TextBox)
                UpdateString.Append("ClicksPurchased=" & MyTextBox.Text)

                'ID
                MyHiddenField = CType(row.FindControl("ID_field"), HiddenField)
                UpdateString.Append(" WHERE ID=" & MyHiddenField.Value)

            Catch
                'Try the next row
            End Try
        Next row

        Me.SqlDataSource1.UpdateCommand = UpdateString.ToString
    End Sub

End Class

